package controller;
import dao.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.*;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetStudentInfo
 */

@WebServlet("/web_CheckLoginServlet")
public class web_CheckLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 2L;
    public web_CheckLoginServlet(){
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name=request.getParameter("username");
        String password=request.getParameter("password");
        String entity=request.getParameter("entity");
        if(entity.equals("2")){
            SqlSession sqlSession;
            String resource = "mybatis-config.xml";// 读取配置文件
            InputStream is = Resources.getResourceAsStream(resource);// 构建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);// 获取sqlSession
            sqlSession = sqlSessionFactory.openSession();
            web_userDao webuserDao = new web_userDaoImpl(sqlSession);
            web_Admin web_admin = webuserDao.queryweb_AdminById(name);
            if(web_admin.getPassword().equals(password)){
                String forwardUrl="web_MainForm.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }else{
                String forwardUrl="web_login.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }

        }else if(entity.equals("0")){
            SqlSession sqlSession;
            String resource = "mybatis-config.xml";// 读取配置文件
            InputStream is = Resources.getResourceAsStream(resource);// 构建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);// 获取sqlSession
            sqlSession = sqlSessionFactory.openSession();
            web_userDao webuserDao = new web_userDaoImpl(sqlSession);
            web_Student web_student = webuserDao.queryweb_StudentById(name);
            if(web_student.getPassword().equals(password)){
                String forwardUrl="web_MainForm.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }else{
                String forwardUrl="web_login.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }
        }else if(entity.equals("1")){//教师
            SqlSession sqlSession;
            String resource = "mybatis-config.xml";// 读取配置文件
            InputStream is = Resources.getResourceAsStream(resource);// 构建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);// 获取sqlSession
            sqlSession = sqlSessionFactory.openSession();
            web_userDao webuserDao = new web_userDaoImpl(sqlSession);
            web_Teacher web_teacher = webuserDao.queryweb_TeacherById(name);
            if(web_teacher.getPassword().equals(password)){
                String forwardUrl="web_MainForm.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }else{
                String forwardUrl="web_login.jsp";
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher(forwardUrl);
                dispatcher.forward(request, response);
            }
        }
    }
}

















